package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.PrUserBankCardDTO;
import com.financing.wap.service.IPrUserBankCardService;

@Controller("prUserBankCardController")
@RequestMapping("/api/prUserBankCard")
public class PrUserBankCardController extends BaseController {

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IPrUserBankCardService prUserBankCardService;
	
	@RequestMapping("/listPrUserBankCard")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, PrUserBankCardDTO prUserBankCard,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query prUserBankCard list begin .......");
			
			prUserBankCard.setBegin(pager.getStartIndex());
			prUserBankCard.setPageSize(pager.getPageSize());
			
			int count = prUserBankCardService.queryListCount(prUserBankCard);
			
			pager.setTotalCount(count);
			
			List<PrUserBankCardDTO> prUserBankCardList = null;
			
			if(count > 0){
				prUserBankCardList = prUserBankCardService.queryList(prUserBankCard);
			}else{
				prUserBankCardList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", prUserBankCardList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query prUserBankCard list end .......");
		}catch(Exception e){
			log.error("query prUserBankCard error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addPrUserBankCard")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, PrUserBankCardDTO prUserBankCard){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add prUserBankCard  begin .......");
			
			DTOdateSetUtil.setCreateInfo(prUserBankCard);
			DTOdateSetUtil.setUpdateInfo(prUserBankCard);
			
			int id = prUserBankCardService.insert(prUserBankCard);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add prUserBankCard list end .......");
		}catch(Exception e){
			log.error("add prUserBankCard error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailPrUserBankCard")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, PrUserBankCardDTO prUserBankCard){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail prUserBankCard  begin .......");
			
			Integer id = prUserBankCard.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			PrUserBankCardDTO dto = prUserBankCardService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail prUserBankCard  end .......");
		}catch(Exception e){
			log.error("query detail prUserBankCard error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deletePrUserBankCard")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, PrUserBankCardDTO prUserBankCard){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete prUserBankCard  begin .......");
			
			Integer id = prUserBankCard.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			prUserBankCardService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete prUserBankCard  end .......");
		}catch(Exception e){
			log.error("delete prUserBankCard error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updatePrUserBankCard")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, PrUserBankCardDTO prUserBankCard){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update prUserBankCard  begin .......");
			
			Integer id = prUserBankCard.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(prUserBankCard);
			
			prUserBankCardService.update(prUserBankCard);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update prUserBankCard list end .......");
		}catch(Exception e){
			log.error("update prUserBankCard error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForcePrUserBankCard")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, PrUserBankCardDTO prUserBankCard){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce prUserBankCard  begin .......");
			
			Integer id = prUserBankCard.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(prUserBankCard);
			
			prUserBankCardService.updateForce(prUserBankCard);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce prUserBankCard list end .......");
		}catch(Exception e){
			log.error("updateForce prUserBankCard error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
